package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yidu.ruifeng_logistics.entity.LogisticsClient;
import com.yidu.ruifeng_logistics.entity.LogisticsModel;
import com.yidu.ruifeng_logistics.service.LogisticsModelService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.LogisticsModelFllVo;
import com.yidu.ruifeng_logistics.vo.LogisticsModelVo;
import com.yidu.ruifeng_logistics.vo.LogisticsModuleVoRe;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsModel)表控制层
 *
 * @author 张皓超
 * @since 2021-03-09 14:24:40
 */
@Api(description = "瑞丰物流模块接口")
@RestController
@RequestMapping("logisticsModel")
public class LogisticsModelController {


    @Resource
    RedisTemplate redisTemplate ;//redis
    /**
     * 服务对象
     */
    @Resource
    private LogisticsModelService logisticsModelService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsModel selectOne(Integer id) {
        return this.logisticsModelService.queryById(id);
    }


    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation(value = "分页显示", notes = "分页返回所有信息")
    @GetMapping("findAll")
    public Map findAll(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);//分页助手
        List<LogisticsModelFllVo> logisticsModelFllVos =logisticsModelService.selectReturnVo(new LogisticsModel());//查询返回vo
        PageInfo<LogisticsModelFllVo> info = new PageInfo<>(logisticsModelFllVos);
        Map<String, Object> map = new HashMap();
        map.put("msg", "查询成功");
        map.put("count", info.getTotal());
        map.put("data", logisticsModelFllVos);
        map.put("code", 0);

        return map;
    }

    /**
     * 删除
     * @param modelId
     * @return
     */
    @ApiOperation(value = "根据id删除", notes = "返回base")
    @PostMapping("deleteId")
    public BaseMessage deleteId(Integer modelId) {
        boolean b = logisticsModelService.deleteById(modelId);//传入id删除
        return b==true?BaseMessage.success():BaseMessage.success();
    }

    @ApiOperation(value = "增加和修改", notes = "返回操作成功")
    @PostMapping("addAndUpdate")
    public BaseMessage addAndUpdate(Integer modelId, //传入id
                                    String modelName,//传入name
                                    String modelRemark,//传入备注
                                    String modelUrl,//传入url
                                    Integer tree1){//传入上级模块
        int rows=this.logisticsModelService.addAndUpdate(modelId,modelName,modelRemark,modelUrl,tree1);//调用事务 在service中处理
        return rows>0?BaseMessage.success():BaseMessage.fail();
    }
    
    @ApiOperation(value = "数据回显", notes = "返回数据")
    @PostMapping("show")
    public BaseMessage show(Integer id){
        if (id!=null){//不等于空就查询
            LogisticsModel logisticsModel1 = logisticsModelService.queryById(id);
            return BaseMessage.success(logisticsModel1);
        }else {//等于空就不查询 返回去
            return BaseMessage.fail();
        }
    }

    @ApiOperation(value = "从数据库拿上级模块", notes = "返回树形数据list")
    @GetMapping("selectShow")
    public List<LogisticsModuleVoRe> selectShow(){
        List<LogisticsModuleVoRe> selectShow =(List<LogisticsModuleVoRe>) redisTemplate.opsForValue().get("selectShow");//取出redis
        if (selectShow==null) { //redis 如果等于空 就不运行下面
            List<LogisticsModuleVoRe> listvo = new ArrayList<>();//页面需要德lisivo
            List<LogisticsModel> list = logisticsModelService.selectI(null);//查询根目录
            for (int i = 0; i < list.size(); i++) {
                LogisticsModuleVoRe moduleVo = new LogisticsModuleVoRe();
                moduleVo.setId(list.get(i).getModelId());
                moduleVo.setName(list.get(i).getModelName());
                moduleVo.setOpen(false);
                moduleVo.setChildren(selectF(list.get(i).getModelId()));//递归
                listvo.add(moduleVo);
            }
            redisTemplate.opsForValue().set("selectShow", listvo);
            return listvo;
        }//不等于空就返回
        return selectShow;
    }

    /**
     * 树形递归
     *
     * @param roleId
     * @return
     */
    public List<LogisticsModuleVoRe> selectF(Integer roleId) {//传入模块id
        List<LogisticsModuleVoRe> list = new ArrayList<>();
        List<LogisticsModel> modelList = logisticsModelService.selectI(roleId);//传入模块id 查询上级模块字段 返回list
        for (int i = 0; i < modelList.size(); i++) {
            LogisticsModuleVoRe logisticsModuleVoRe = new LogisticsModuleVoRe();//new vo
            logisticsModuleVoRe.setId(modelList.get(i).getModelId());
            logisticsModuleVoRe.setName(modelList.get(i).getModelName());
            logisticsModuleVoRe.setOpen(false);
            if (roleId != modelList.get(i).getModelParent()) {//如果模块id 不等于 循环对象德上级模块id 就返回出去
                return list;
            } else { //否则就就继续
                logisticsModuleVoRe.setChildren(selectF(modelList.get(i).getModelId()));//递归
                list.add(logisticsModuleVoRe);
            }
        }
        return list;
    }
}
